<?php
/*
 * 产品入库
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Inventory;
use app\adminbackstage\model\InventoryTotal;
use app\adminbackstage\model\User;
use app\adminbackstage\model\Product;

use think\facade\Db;

class InventoryController extends BaseController{

    //入库列表
    public function inventoryList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $w_name = Request::param('w_name');


        $where = [];
        $where[] = ['i.enable','=','1'];
        if(!empty($w_name)){
            $where[] = ['w.w_name','like','%'.$w_name.'%'];
        }

        if(!empty($p_name)){
            $where[] = ['p.p_name','like','%'.$p_name.'%'];
        }

        $list = Inventory::inventoryList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Inventory::inventoryCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //入库添加
    public function inventoryAdd(){
        
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $p_id = Request::param('p_id');
        $w_id = Request::param('w_id');
        $ware_quantity = Request::param('ware_quantity');
        $user = new User();
        
        $where_u = [];
        $where_u[] = ['u.uid','=',$a_uid];
        $find = $user->find($where_u);
        
        $operator = $find['username'];
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [

            'p_id' => $p_id,
            'enable' => $enable,
            'addtime' => $time,
            'w_id' => $w_id,
            'uid' => $a_uid,
            'operator' => $operator,
            'ware_quantity' => $ware_quantity,


        ];

        Inventory::inventoryAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //仓库详情
    /*public function inventoryInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Warehouse::warehouseInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);


       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }*/
    
    //入库修改
    public function inventoryEdit(){
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $p_id = Request::param('p_id');
        $w_id = Request::param('w_id');
        $ware_quantity = Request::param('ware_quantity');
        $state = Request::param('state');
        $user = new User();
        
        $where_u = [];
        $where_u[] = ['u.uid','=',$a_uid];
        $find = $user->find($where_u);
        
        $operator = $find['username'];
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        
        $update = [];
        if(!empty($p_id)){
            $update['p_id'] = $p_id;
        }
        
        if(!empty($w_id)){
            $update['w_id'] = $w_id;
        }
        
        if(!empty($operator)){
            $update['operator'] = $operator;
        }
        
        if(!empty($uid)){
            $update['uid'] = $a_uid;
        }
        
        if(!empty($state)){
            $update['state'] = $state;
        }
        
        if(!empty($ware_quantity)){
            $update['ware_quantity'] = $ware_quantity;
        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }
        if($state==2){
            $where = [];
            $where[] = ['id','=',$id];
            $where[] = ['enable','=',1];
            $inventoryInfo = Inventory::inventoryInfo($where);
            
            if($inventoryInfo['state']==1){
                Db::startTrans();
                try {
                    $time = time();

                    $sql = 'update wa_product set inventory=inventory+'.$ware_quantity.' where id='.$p_id ;
                    Product::ruleSql($sql);
                    
                    $where_t = [];
                    $where_t[] = ['p_id','=',$inventoryInfo['p_id']];
                    $where_t[] = ['w_id','=',$inventoryInfo['w_id']];
                    $where_t[] = ['enable','=',1];
                    $inventoryTotalInfo = InventoryTotal::inventoryTotalInfo($where_t);
                    
                    if(!empty($inventoryTotalInfo)){
                        $sql_t = 'update wa_inventory_total set ware_quantity=ware_quantity+'.$ware_quantity.' where id='.$inventoryTotalInfo['id'] ;
                        Product::ruleSql($sql_t);
                    }
                    else{
                        $time = time();
                        $data = [

                            'p_id' => $inventoryInfo['p_id'],
                            'enable' => 1,
                            'addtime' => $time,
                            'w_id' => $inventoryInfo['w_id'],
                            'uid' => $a_uid,
                            'ware_quantity' => $ware_quantity,
                        ];
                        InventoryTotal::inventoryTotalAdd($data);
                    }
 

                    Db::commit();
                }catch (\Exception $e){
                    Db::rollback();
                }


            }

        }

        Inventory::inventoryEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    
    
    //库存总数列表
    public function inventoryTotalList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $w_name = Request::param('w_name');


        $where = [];
        $where[] = ['i.enable','=','1'];
        if(!empty($w_name)){
            $where[] = ['w.w_name','like','%'.$w_name.'%'];
        }

        if(!empty($p_name)){
            $where[] = ['p.p_name','like','%'.$p_name.'%'];
        }

        $list = InventoryTotal::inventoryTotalList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = InventoryTotal::inventoryTotalCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }

}
